package com.anolesoft.yr.mbus.sup.mapper;

import com.anolesoft.yr.mbus.sup.entity.SupBasePartPriceDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/12 9:20
 */
@Mapper
public interface SupBasePartPriceMapper {
    @Insert("insert into sup_base_part_price (" +
            "objuid," +
            "fac_code," +
            "sup_base_part_uid," +
            "offer_person," +
            "offer_time," +
            "offer_price," +
            "deal_price," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{supBasePartUid,jdbcType=VARCHAR}," +
            "#{offerPerson,jdbcType=VARCHAR}," +
            "#{offerTime,jdbcType=TIMESTAMP}," +
            "#{offerPrice,jdbcType=DECIMAL}," +
            "#{dealPrice,jdbcType=DECIMAL}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(SupBasePartPriceDO supBasePartPriceDO);

    @Update("update sup_base_part_price set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "sup_base_part_uid = #{supBasePartUid,jdbcType=VARCHAR}," +
            "offer_person = #{offerPerson,jdbcType=VARCHAR}," +
            "offer_time = #{offerTime,jdbcType=TIMESTAMP}," +
            "offer_price = #{offerPrice,jdbcType=DECIMAL}," +
            "deal_price = #{dealPrice,jdbcType=DECIMAL}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR}" +
            " where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(SupBasePartPriceDO supBasePartPriceDO);

    @Update("update sup_base_part_price set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "SupBasePartPriceResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sup_base_part_uid", property = "supBasePartUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "offer_person", property = "offerPerson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "offer_time", property = "offerTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "offer_price", property = "offerPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_price", property = "dealPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sup_base_part_uid", property = "supBasePart", one = @One(select = "com.anolesoft.yr.mbus.sup.mapper.SupBasePartMapper.selectOneById",fetchType = FetchType.LAZY)),
    })
    @Select("select " +
            "objuid, fac_code, " +
            "sup_base_part_uid, offer_person, offer_time, offer_price, deal_price," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from sup_base_part_price " +
            "where del_flag = 0")
    List<SupBasePartPriceDO> selectAll();

    @ResultMap("SupBasePartPriceResultMap")
    @Select("select " +
            "objuid, fac_code, " +
            "sup_base_part_uid, offer_person, offer_time, offer_price, deal_price," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from sup_base_part_price " +
            "where del_flag = 0")
    List<SupBasePartPriceDO> selectPageAll(Page<SupBasePartPriceDO> page);

    @ResultMap("SupBasePartPriceResultMap")
    @Select("select " +
            "objuid, fac_code, " +
            "sup_base_part_uid, offer_person, offer_time, offer_price, deal_price," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from sup_base_part_price " +
            "where del_flag = 0 and objuid = #{id,jdbcType=VARCHAR}")
    SupBasePartPriceDO selectOneById(@Param("id") String id);

    @Select("<script>" +
            "select " +
            "objuid, fac_code, " +
            "sup_base_part_uid, offer_person, offer_time, offer_price, deal_price," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from sup_base_part_price " +
            "where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.supBasePartUid !=null and o.supBasePartUid != '' \">" +
            "   and sup_base_part_uid = #{o.supBasePartUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.offerPerson !=null and o.offerPerson != '' \">" +
            "   and offer_person = #{o.offerPerson, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    @ResultMap("SupBasePartPriceResultMap")
    List<SupBasePartPriceDO> selectList(@Param("o") SupBasePartPriceDO supBasePartPriceDO);

    @Select("<script>" +
            "select " +
            "objuid, fac_code, " +
            "sup_base_part_uid, offer_person, offer_time, offer_price, deal_price," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from sup_base_part_price " +
            "where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.supBasePartUid !=null and o.supBasePartUid != '' \">" +
            "   and sup_base_part_uid = #{o.supBasePartUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.offerPerson !=null and o.offerPerson != '' \">" +
            "   and offer_person = #{o.offerPerson, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    @ResultMap("SupBasePartPriceResultMap")
    List<SupBasePartPriceDO> selectPageList(@Param("o") SupBasePartPriceDO supBasePartPriceDO, Page<SupBasePartPriceDO> page);

    List<SupBasePartPriceDO> selectPageConditionList(@Param("o") SupBasePartPriceDO supBasePartPriceDO, Page<SupBasePartPriceDO> page);
}
